package cn.wetcland.urban.framework.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.wetcland.urban.common.AjaxResponse;
import cn.wetcland.urban.framework.annotation.SaCheckDriverLogin;
import cn.wetcland.urban.framework.dto.CustomerCreateOrderDTO;
import cn.wetcland.urban.framework.dto.CustomerDeleteOrderListDTO;
import cn.wetcland.urban.framework.dto.CustomerEvaluationDTO;
import cn.wetcland.urban.framework.service.UtOrderService;
import cn.wetcland.urban.framework.utils.StpDriverUtil;
import cn.wetcland.urban.framework.vo.*;
import cn.wetcland.urban.generator.dto.BaseConditionSearchDTO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Driver;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    public UtOrderService orderService;

    @SaCheckLogin
    @PostMapping("/customerCreateOrder")
    public AjaxResponse customerCreateOrder(@Validated @RequestBody CustomerCreateOrderDTO customerCreateOrderDTO) {
        orderService.handleCustomerCreateOrder(customerCreateOrderDTO);
        return AjaxResponse.success();
    }

    @SaCheckLogin
    @DeleteMapping("/customerDeleteOrder")
    public AjaxResponse customerDeleteOrder(@Validated @RequestBody CustomerDeleteOrderListDTO deleteOrderListDTO) {
        orderService.handleCustomerDeleteOrder(deleteOrderListDTO);
        return AjaxResponse.success();
    }

    @SaCheckLogin
    @GetMapping("/customerOrderList/{type}")
    public AjaxResponse customerOrderList(@PathVariable Integer type, BaseConditionSearchDTO searchDTO) {
        Page<CustomerOrderListItemVO> customerOrderListItemVOIPage = orderService.customerOrderList(type, searchDTO);
        return AjaxResponse.success(customerOrderListItemVOIPage);
    }

    @SaCheckLogin
    @GetMapping("/customerOrderDetails/{id}")
    public AjaxResponse customerOrderDetails(@PathVariable Long id) {
        CustomerOrderDetailsVO customerOrderDetailsVO = orderService.customerOrderDetails(id);
        return AjaxResponse.success(customerOrderDetailsVO);
    }

    @SaCheckDriverLogin
    @GetMapping("/driverGainOrderList")
    public AjaxResponse driverGainOrderList(BaseConditionSearchDTO searchDTO) {
        Page<DriverGainOrderListItemVO> driverGainOrderListItemVOPage = orderService.handleDriverGainOrderList(searchDTO);
        return AjaxResponse.success(driverGainOrderListItemVOPage);
    }

    @SaCheckDriverLogin
    @PutMapping("/driverGainOrder/{orderId}")
    public AjaxResponse driverGainOrder(@PathVariable Long orderId) {
        orderService.handleDriverGainOrder(orderId);
        return AjaxResponse.success();
    }

    /**
     *
     * @param type 0已接单 1待取货 2待卸货 3完成
     * @param searchDTO 搜索
     * @return VO
     */
    @SaCheckDriverLogin
    @GetMapping("/driverOrderList/{type}")
    public AjaxResponse driverOrderList(@PathVariable Integer type, BaseConditionSearchDTO searchDTO) {
        Page<DriverOrderListItemVO> driverOrderList = orderService.driverOrderList(type, searchDTO);
        return AjaxResponse.success(driverOrderList);
    }

    /**
     * 与customerOrderDetails功能相同，只是替换了验证司机登录
     * @param id 订单ID
     * @return VO
     */
    @SaCheckDriverLogin
    @GetMapping("/driverOrderDetails/{id}")
    public AjaxResponse driverOrderDetails(@PathVariable Long id) {
        CustomerOrderDetailsVO customerOrderDetailsVO = orderService.customerOrderDetails(id);
        return AjaxResponse.success(customerOrderDetailsVO);
    }

    @SaCheckDriverLogin
    @GetMapping("/driverOrderMap/{id}")
    public AjaxResponse driverOrderMap(@PathVariable Long id) {
        DriverOrderMapVO orderMapVO = orderService.driverOrderMap(id);
        return AjaxResponse.success(orderMapVO);
    }

    @SaCheckLogin
    @GetMapping("/customerOrderMap/{id}")
    public AjaxResponse customerOrderMap(@PathVariable Long id) {
        DriverOrderMapVO orderMapVO = orderService.driverOrderMap(id);
        return AjaxResponse.success(orderMapVO);
    }

    /**
     * 0等待接单 1待取货 2待收货 3订单完成 4用户已评价
     * 用户创建订单(0) - 司机接单(1) - 司机已取货(2) - 司机已卸货(3) - 用户已评价(4)
     * @param orderId 订单号
     * @return RES
     */
    @SaCheckDriverLogin
    @PutMapping("/driverOrderGainGoods/{orderId}")
    public AjaxResponse driverOrderGainGoods(@PathVariable Long orderId) {
        orderService.driverOrderGainGoods(orderId);
        return AjaxResponse.success();
    }

    @SaCheckDriverLogin
    @PutMapping("/driverOrderDropGoods/{orderId}")
    public AjaxResponse driverOrderDropGoods(@PathVariable Long orderId) {
        orderService.driverOrderDropGoods(orderId);
        return AjaxResponse.success();
    }

    @SaCheckLogin
    @PostMapping("/customerOrderConfirm")
    public AjaxResponse customerOrderConfirmEvaluation(@RequestBody CustomerEvaluationDTO evaluationDTO) {
        orderService.customerOrderConfirmEvaluation(evaluationDTO);
        return AjaxResponse.success();
    }

    @SaCheckLogin
    @PutMapping("/customerOrderCancel/{orderId}")
    public AjaxResponse customerOrderCancel(@PathVariable Long orderId) {
        orderService.customerOrderCancel(orderId);
        return AjaxResponse.success();
    }

}
